//
//  BMNavBar.m
//  Baymax
//
//  Created by jolin on 16/4/18.
//  Copyright © 2016年 jolin. All rights reserved.
//

#import "BMNavBar.h"
#import "Header.h"

#define NAVBARHIGHT 64.0

@implementation BMNavBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithItems:(NSArray *)items navBarStyle:(NavBarStyle)style {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, MAINFRAME.size.width, NAVBARHIGHT);
        self.backgroundColor = [UIColor lightGrayColor];
        
        self.items = items;
        self.style = style;
        
        //添加navBarItem
        [self setUpNavBarItem];
    }
    return self;
}

#pragma mark - 添加导航栏项目
- (void)setUpNavBarItem {
   // CGFloat img_X = 0.0f;//记录项目坐标
    //判断Item
    for (int i = 0; i < _items.count; i++) {
        UIButton *navBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        navBtn.tag = 100 + i;
        if ([_items[i] hasSuffix:@".jpg"]||[_items[i] hasSuffix:@".png"]) {
            //图片
            UIImage *btnImg = [UIImage imageNamed:_items[i]];
           
            navBtn.frame = i==0?CGRectMake(20, 15, 35, 35):CGRectMake(self.frame.size.width-50, 15, 35, 35);
            
            [navBtn setImage:btnImg forState:UIControlStateNormal];
            
        }else if(_items[i] != nil) {
            
            navBtn.frame = i == 0?CGRectMake(0, 15, 100, 35):i==1?CGRectMake(self.frame.size.width/2.0-50, 15, 100, 35):CGRectMake(self.frame.size.width-100, 10, 100, 35);
            [navBtn setTitle:_items[i] forState:UIControlStateNormal];
//            UILabel *titleLabel = [[UILabel alloc]initWithFrame:i == 0?CGRectMake(0, 15, 100, 35):i==1?CGRectMake(self.frame.size.width/2.0-50, 15, 100, 35):CGRectMake(self.frame.size.width-100, 10, 100, 35)];
//            if (_items.count == 1) {
//                titleLabel.frame = CGRectMake(self.frame.size.width/2.0-50, 15, 100, 35);
//            }
//            titleLabel.text = _items[i];
//            titleLabel.textAlignment = NSTextAlignmentCenter;
//            titleLabel.tag = 100 + i;
//            titleLabel.font = [UIFont systemFontOfSize:18 weight:0.5];
//            [self addSubview:titleLabel];
        }
        if (_items.count == 1) {
            navBtn.frame = CGRectMake(self.frame.size.width/2.0-50, 15, 100, 35);
        }
        [navBtn addTarget:self action:@selector(navButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:navBtn];

    }
}

#pragma mark - 导航按钮点击事件
- (void)navButtonClick:(UIButton *)sender {
    if (self.itemBlock) {
        self.itemBlock(sender.tag - 100, sender);
    }
}

@end
